package com.dgshanger.sanhxiaofeisc.ui.person.purse

import android.annotation.SuppressLint
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.entity.HistoryTransferEntity
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.adapter.BaseRecycleAdapter
import com.xx.baseuilibrary.adapter.BaseViewHolder
import com.xx.baseuilibrary.mvp.contract.BaseMvpRecycleViewContract
import com.xx.baseuilibrary.mvp.lcec.BaseMvpRecycleViewFragment
import com.xx.baseutilslibrary.network.entity.BaseResponseEntity
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import io.reactivex.Observable
import kotlinx.android.synthetic.main.item_history_aid_list.view.*


/**
 * HistoryPaidListFragment
 * 沉迷学习不能自拔
 * Describe：历史转账列表
 * Created by 雷小星🍀 on 2018/4/18 13:38.
 */
class HistoryTransferListFragment : BaseMvpRecycleViewFragment<HistoryTransferEntity>() {

    override val listLimit: Int
        get() = 10

    override val layoutManager: RecyclerView.LayoutManager
        get() =
//            LinearLayoutManager(mContext)
            object : LinearLayoutManager(mContext) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }

    override fun createAdapter(): BaseRecycleAdapter<HistoryTransferEntity, out BaseViewHolder<HistoryTransferEntity>> = Adapter()

    override fun createModel(): BaseMvpRecycleViewContract.Model<HistoryTransferEntity> = Model()

    inner class Model : BaseMvpRecycleViewContract.Model<HistoryTransferEntity> {
        override fun loadData(page: Int, httpObserver: XxBaseHttpObserver<List<HistoryTransferEntity>>?) {

            AppApi.Api()
                    .transfer_detail()
                    .flatMap({ myTransferAccountsEntity ->
                        Observable.just(BaseResponseEntity(1, myTransferAccountsEntity.data!!.user_transfer_record))
                    })
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)
        }

    }

    override fun initEvent(view: View?) {
        super.initEvent(view)
        //设置上层回调
        adapter?.setOnItemClickListener { _, entity ->
            onItemClickListener?.onItemClick(entity)
        }
    }

    class Adapter : BaseRecycleAdapter<HistoryTransferEntity, ViewHolder>() {

        override fun createViewHolder(view: View, viewType: Int): ViewHolder = ViewHolder(view)

        override fun getItemLayout(viewType: Int): Int = R.layout.item_history_aid_list
    }

    class ViewHolder(view: View) : BaseViewHolder<HistoryTransferEntity>(view) {
        @SuppressLint("SetTextI18n")
        override fun bindView(position: Int, entity: HistoryTransferEntity?) {
            with(entity!!) {
                itemView.tv_payee_user_show_id.text = "ID:" + payee_user_show_id
                itemView.tv_payee_user_nickname.text = payee_user_name
                ImageLoader.loadAvatar(itemView.context, itemView.iv_avatar, payee_user_avatar)
            }
        }

    }

    interface OnItemClickListener {
        fun onItemClick(entity: HistoryTransferEntity)
    }

    var onItemClickListener: OnItemClickListener? = null
}